﻿using System;
using System.Security.Permissions;
using System.Security.Principal;
using System.Threading;
using System.Windows;
using AzManPermissions;

namespace AzManApplication
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    /// <summary>
    /// AzMan operation checking permissions work only when when principal policy is set to PrincipalPolicy.WindowsPrincipal
    /// </summary>
    static MainWindow()
    {
      AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
    }

    public MainWindow()
    {
      InitializeComponent();
      AzManOperationPermission azmanPerm = new AzManOperationPermission(null, (int)Operations.Start);
      azmanPerm.Demand();
      DataContext = new MainWindowViewModel();
    }

    [AzManOperationPermission(SecurityAction.Demand, Operation = (int)Operations.Start)]
    private void SomeAction_Click(object sender, RoutedEventArgs e)
    {
      ((MainWindowViewModel)DataContext).SomeActionLocal();
    }

    private void SomeActionRemote_Click(object sender, RoutedEventArgs e)
    {
      ((MainWindowViewModel)DataContext).SomeActionRemote();
    }

    private void OtherAction_Click(object sender, RoutedEventArgs e)
    {
      ((MainWindowViewModel)DataContext).OtherAction();
    }

    private void OtherActionRemote_Click(object sender, RoutedEventArgs e)
    {
      ((MainWindowViewModel)DataContext).OtherActionRemote();
    }

    private void UserNamesInRole_Click(object sender, RoutedEventArgs e)
    {
      ((MainWindowViewModel)DataContext).GetUserNames();
    }

    private void UsersInRole_Click(object sender, RoutedEventArgs e)
    {
      ((MainWindowViewModel)DataContext).GetUsers();
    }

    private void AppMembersInRole_Click(object sender, RoutedEventArgs e)
    {
      ((MainWindowViewModel)DataContext).GetAppMembers();
    }

    private void UpdateCache_Click(object sender, RoutedEventArgs e)
    {
      ((MainWindowViewModel)DataContext).UpdateCache();
    }

    private void AllUsersInRole_Click(object sender, RoutedEventArgs e)
    {
      ((MainWindowViewModel)DataContext).GetAllUsers();
    }
  }
}